﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///Log 的摘要说明
/// </summary>
public partial class ActionLog
{
    public enum typeUser { 后台用户 = 1 }
    public static ActionLog GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ActionLog.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<ActionLog> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ActionLog.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.ActionLog.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<ActionLog>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.ActionLog.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.ActionLog.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static List<ActionLog> GetAllByConditon(int typeUser, string AddTimeForm, string AddTimeTo, int UserID, string Action)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.ActionLog
                        where t.Enable_Flag == 1
                        select t;
            if (typeUser != 0)
                query = query.Where(row => row.Type == typeUser);
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            if (UserID != 0)
                query = query.Where(row => row.AddUserID == UserID);
            if (!string.IsNullOrEmpty(Action))
                query = query.Where(row => row.Action.Contains(Action));
            query = query.OrderByDescending(row => row.AddTime);
            return query.ToList();
        }
    }

    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            ActionLog objLog = ActionLog.GetByID(ID);
            if (objLog == null)
                return;
            objLog.Enable_Flag = 0;
            objLog.Update();
        }
    }
}